# Copyright (c) 2023, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc

class SRMNoCodeInquiry(Document):
	@frappe.whitelist()
	def create_nocodeinquiry_of_supplier(self):
		for s in self.supplier_subs:
			self.make_nocodeinquiry_of_supplier(self.name, s.supplier_number, s.supplier_name, s.receive_email)

	# 生成无物料编码供应商询价单，每个供应商一张单以隔离权限
	@frappe.whitelist()
	def make_nocodeinquiry_of_supplier(self, source_name, supplier, supplier_name, receive_person, target_doc=None):
		doc = get_mapped_doc(
			"SRM NoCodeInquiry",
			source_name,
			{
				"SRM NoCodeInquiry": {
					"doctype": "SRM NoCodeInquiry Of Supplier",
					"field_map": {"name": "from_inquiry"},
					"validation": {
						"current_status": ["=", "待处理"],
					},
				},
				"SRM NoCodeInquiry Material Sub": {
					"doctype": "SRM NoCodeInquiry Of Supplier Material Sub",
					"field_map": {
						"qty": "qty",
						"name": "source_entry",
					},
				},
			},
			target_doc,
			ignore_permissions=True
		)
		doc.supplier = supplier
		doc.supplier_name = supplier_name
		doc.receive_person = receive_person
		doc.insert(ignore_permissions=True)
		return doc

